﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H01
{
    class Program
    {
        static void Main(string[] args)
        {
            #region  编写程序，分别用 while、do...while和for循环来计算数列1+1/2+2/3+...+99/100
            void T1()
            {
                Console.WriteLine("T1.编写程序，分别用 while、do...while和for循环来计算数列1+1/2+2/3+...+99/100");
                int i = 1;
                double sum = 1.0;

                //while语句
                while (i <= 100)
                {
                    sum = sum + (i - 1) * 1.0 / i;
                    i++;
                }
                Console.WriteLine("while语句:\n1+1/2+2/3+...+99/100={0}", sum);

                //do...while语句
                i = 1;
                sum = 1.0;
                do
                {
                    sum = sum + (i - 1) * 1.0 / i;
                    i++;
                }
                while (i <= 100);
                Console.WriteLine();
                Console.WriteLine("do...while语句:\n1+1/2+2/3+...+99/100={0}", sum);

                //for循环
                i = 1;
                sum = 1.0;
                for (; i <= 100; i++)
                {
                    sum = sum + (i - 1) * 1.0 / i;
                }
                Console.WriteLine();
                Console.WriteLine("for循环:\n1+1/2+2/3+...+99/100={0}", sum);

            }

            #endregion

            #region  斐波那契（Fibonacci）数列的前两项，a[0]=1,a[1]=1，之后每一项a[n]=a[n-1]+a[n-2]。试写出计算数列第n项的非递归方法代码
            void T2()
            {
                Console.WriteLine("T2.斐波那契（Fibonacci）数列的前两项，a[0]=1,a[1]=1，之后每一项a[n]=a[n-1]+a[n-2]。试写出计算数列第n项的非递归方法");
                int[] a = new int[100];
                a[0] = a[1] = 1;
                Console.Write("求取第N项(-1退出):");
                int n = int.Parse(Console.ReadLine());
                while (n != -1)
                {
                    if ((n == 1) || (n == 2))
                    {
                        Console.WriteLine("a[{0}]=1", n);
                    }
                    else
                    {
                        for (int i = 3; i <= n; i++)
                        {
                            a[i - 1] = a[i - 2] + a[i - 3];
                        }
                        Console.WriteLine("a[{0}]={1}", n, a[n - 1]);
                    }
                    Console.Write("求取第N项(-1退出):");
                    n = int.Parse(Console.ReadLine());
                }

            }

            #endregion

            #region 求2个数的最小公倍数
            void T3()
            {
                Console.WriteLine("T3.求2个数的最小公倍数");
                Console.Write("输入第一个数a:");
                int a = int.Parse(Console.ReadLine());
                int n = a;
                Console.Write("输入第二个数b:");
                int b = int.Parse(Console.ReadLine());
                int m = b;
                int i = 2; int j = 2;

                do
                {
                    if (n < m)
                    {
                        n = a * i;
                        i++;
                    }
                    if (n > m)
                    {
                        m = b * j;
                        j++;
                    }
                    if (n == m)
                    {
                        Console.WriteLine("{0},{1}的最小公倍数:{2}", a, b, n);
                        break;
                    }
                } while (n != m);
            }
           
            #endregion

            T1();
            Console.WriteLine();
            T2();
            Console.WriteLine();
            T3();
            Console.WriteLine();

            Console.ReadKey();


        }
    }
}
